package com.unitd.configclient.environment;

import org.springframework.aop.framework.Advised;
import org.springframework.aop.support.AopUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;

import java.util.Set;

/**
 * @desc 当前运行环境变动监听事件
 * @filename EnvironmentChangeEvent.java
 * @copyright www.pactera.com.cn
 * @author Hudan
 * @version 1.0
 * @date 2018/1/14
 */
public class EnvironmentChangeListener implements ApplicationListener<EnvironmentChangeEvent>, EnvironmentAware {

	private Environment environment;

	@Override
	public void setEnvironment(Environment environment) {
		this.environment = environment;
	}

	@Override
	public void onApplicationEvent(EnvironmentChangeEvent event) {
		Set<String> keys = event.getKeys();
	}

	@SuppressWarnings({"unchecked", "unused"})
	private static <T> T getTargetObject(Object candidate) {
		try {
			if (AopUtils.isAopProxy(candidate) && (candidate instanceof Advised)) {
				return (T) ((Advised) candidate).getTargetSource().getTarget();
			}
		} catch (Exception ex) {
			throw new IllegalStateException("Failed to unwrap proxied object", ex);
		}
		return (T) candidate;
	}
}